// pages/craftsman_index/craftsman_index.js
const WxParse = require('../../lib/wxParse/wxParse.js');
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { setCollectItem, getItemDetail, getEvaList, createGoodsOrder } from '../../static/api/api.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        itemData:null,
        itemId:'',
        apTime:'',
        isCollected:'',
        otherImg:[],
        listData: null,
        isNoMore: false,
        pageLoad: false,
        curStatus: 1,
        sort_field: 'oie.id',
        usableType: 30,
        curType:10,
        pages: {
            curPage: 1,
            totalPage: 1
        },
        apParams: {
            store_user_id: '',
            set_date: ''
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            'itemId': options.id
        })
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getItemDetail()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '手指的艺术，这款' + this.data.itemData.goods_name + '值得下单！是闺蜜我才分享，推荐你做这款' + this.data.itemData.goods_name +'！',
            path: '/pages/door_service_detail/door_service_detail?id=' + this.data.itemId,
            imageUrl: this.data.itemData.main_img
        }
    },
    setCollectItem() {
        let options = {
            item_id: this.data.itemId,
            behavior: 15
        }
        wx.showLoading({
            title: '加载中...',
        })
        setCollectItem(options).then(response => {
            wx.hideLoading();
            if (response.data.status == 1) {
                wx.showToast({
                    icon: 'none',
                    title: '收藏成功',
                })
            } else {
                wx.showToast({
                    icon: 'none',
                    title: '取消收藏成功',
                })
            }

            this.setData({
                isCollected: response.data.status
            })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    getItemDetail() {
        let that = this;
        let options = {
            goods_id: this.data.itemId
        }
        wx.showLoading({
            title: '加载中...',
        })
        getItemDetail(options).then(response => {
            wx.hideLoading();
            
            this.setData({
                'itemData': response.data.info,
                'otherImg': utils.formatImgArr(response.data.info.other_imgs),
                'isCollected': response.data.info.is_collect,
                'apParams.store_user_id': response.data.info.store_user_id
            })
            this.getEvaList()
            WxParse.wxParse('description', 'html', that.data.itemData.description || '', that);
            WxParse.wxParse('abstract', 'html', that.data.itemData.abstract || '', that);

        }).catch(response => {
            wx.hideLoading();
        })
    },
    createGoodsOrder() {
        let options={};
        if (parseInt(this.data.itemData.store_user_id)>0){
            options = {
                goods_id: this.data.itemId,
                appoint_time:this.data.apTime
            }  
        }else{
            options = {
                goods_id: this.data.itemId
            }
        }
        wx.showLoading({
            title: '加载中...',
        })
        createGoodsOrder(options).then(response => {
            wx.hideLoading();
            console.log('订单信息')
            console.log(response.data)
            setTimeout(function(){
                console.log(`/pages/comfirm_order/comfirm_order?order_id=${response.data.order_id}&order_type=${response.data.order_type}`)
                wx.navigateTo({
                    url: `/pages/comfirm_order/comfirm_order?order_id=${response.data.order_id}&order_type=${response.data.order_type}`,
                })
            },200)
            

        }).catch(response => {
            wx.hideLoading();
        })
    },
    //预览图片
    handleImagePreview(e) {
        const dataset = e.currentTarget.dataset
        wx.previewImage({
            current: dataset.src,  //当前预览的图片
            urls: dataset.images,  //所有要预览的图片
        })
    },
    goIndex(){
        if (this.data.itemData.store_user_id>0){
            wx.navigateTo({
                url: '/pages/craftsman_index/craftsman_index?id=' + this.data.itemData.store_id,
            })
        }else{
            wx.navigateTo({
                
                url: '/pages/shop_detail_index/shop_detail_index?id=' + this.data.itemData.store_user_id,
            })
        }
    },
    goIndex(e) {
        wx.switchTab({
            url: '/pages/shop_index/shop_index',
        })
    },
    getEvaList() {
        if (this.data.pageLoad) return;
        if (this.data.pages.curPage > this.data.pages.totalPage) return;
        this.setData({
            pageLoad: true
        })
        let that = this;
        let params = {
            goods_id: this.data.itemId,
            //store_id:30,
            page: this.data.pages.curPage,
            sort_field: this.data.sort_field,
            sort_type: 'desc'
        }
        getEvaList(params).then(response => {
            let data = response.data;
            if (that.data.pages.curPage == 1) {
                that.setData({
                    listData: data.lists
                })
            } else {
                that.setData({
                    listData: that.data.listData.concat(data.lists)
                })
            }
            that.setData({
                'pages.curPage': that.data.pages.curPage + 1,
                'pages.totalPage': data.page.total_page,
                'pageLoad': false
            })
            if (this.data.pages.curPage >= this.data.pages.totalPage) {
                this.setData({
                    isNoMore: true
                })
            };
        }).catch(response => {
            that.setData({
                'pageLoad': false
            })
        })

    },
    changeType(e) {
        let dataset = e.currentTarget.dataset;
        this.setData({
            'curType': dataset.type
        })
        this.setData({
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            'listData': null
        })
        if (this.data.curType == 10) {

            this.getEvaList()
        }
    },
    onReachBottom() {
        console.log(123)
        this.getEvaList()
    },
    changeEvaType(e) {
        const dataset = e.currentTarget.dataset
        this.setData({
            'sort_field': dataset.key
        })
        this.setData({
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            'listData': null
        })
        this.getEvaList()
    },
    goUrl(e) {
        utils.goNavigationUrl(e)
    },
    getAppointTime(e){
        console.log(e)
        this.setData({
            apTime:e.detail.date
        })
    },
    goDetail(e){
        let dataset = e.currentTarget.dataset;
        if(dataset.type==20){
            wx.navigateTo({
                url: '/pages/craftsman_index/craftsman_index?id='+dataset.store_user_id,
            })
        }else{
            wx.navigateTo({
                url: '/pages/shop_detail_index/shop_detail_index?id=' + dataset.id,
            }) 
        }

    }
})